function lessonGrid()
{
    $('#lessonGrid').jqGrid({
                url: siteUrl + 'lesson/get_lessons',
                mtype: 'post',
                datatype: 'json',
                postData: {
                  group_id: $('#group_id').val()
                },
                colNames: ['ID','Lesson name', 'Group' ,'Operations'],
                colModel: [
                {name: 'lesson_id', index: 'lesson_id', hidden: true, key: true},
                {name: 'lesson_name', index: 'lesson_name'},
                {name: 'group_id', index: 'group_id', hidden: true},
                {name: 'ops', index: 'ops', align: 'center', sortable: false, formatter: lessonOperations}
                ],
            autowidth: true,
            height: '200',
            pager: $('#pagination'),
            sortname: 'group_id',
            sortorder: 'asc',
            viewrecords: true,
            rownumbers: true,
            ondblClickRow: function(rowId, iRow, iCol, e) {
                window.location = siteUrl + 'group/view/' + rowId;
            }
    });
}

function lessonFilesGrid()
{
    $('#lessonFilesGrid').jqGrid({
                url: siteUrl + 'lesson/get_lessons_files',
                mtype: 'post',
                datatype: 'json',
                postData: {
                  lesson_id: $('#lesson_id').val()
                },
                colNames: ['ID','File name', 'Operations'],
                colModel: [
                {name: 'lesson_file_id', index: 'lesson_file_id', hidden: true, key: true},
                {name: 'file_name', index: 'file_name'},
                {name: 'ops', index: 'ops', align: 'center', sortable: false, formatter: lessonFilesOperations}
                ],
            autowidth: true,
            height: '200',
            pager: $('#pagination'),
            sortname: 'lesson_file_id',
            sortorder: 'asc',
            viewrecords: true,
            rownumbers: true
    });
}

function lessonOperations(cell, opts, rowObj)
{
    var id = opts.rowId;
    var group_id = rowObj[2];
    
    var edit = '<a href="' + siteUrl + 'lesson/edit/' + group_id + '/' + id + '" title="Update" class="icon icon-update"></a>';
    edit  = (user_type == 'student') ? '<span class = "icon" ></span>' : edit;

    var upload = '<a href="' + siteUrl + 'lesson/lesson_files/' + group_id + '/' + id + '" title="Upload files" class="icon24 icon-upload"></a>';
    upload = (user_type == 'student') ? '<span class = "icon24" ></span>' : upload;

    var del = '<a href="javascript:$.confirmDelete(' + id + ', \'deleteLesson\', \'Lesson - Delete Record\')" title="Delete" class="icon icon-delete"></a>';
    del = (user_type == 'student') ? '<span class = "icon" ></span>' : del;

    var download = '<a href="' + siteUrl + 'lesson/lesson_download_files/' + group_id + '/' + id + '" title="Download files" class="icon24 icon-down"></a>';
    download = (user_type == 'instructor') ? '<span class = "icon24" ></span>' : download;
    
    return upload+edit+del+download;
}

function lessonFilesOperations(cell, opts, rowObj)
{
    var id = opts.rowId;

    var download = '<a target="_blank" href="' + siteUrl + 'lesson/download_file/' + id + '" title="Download file" class="icon24 icon-down"></a>';
    var del = '<a href="javascript:$.confirmDelete(' + id + ', \'deleteLessonFile\', \'Lesson - Delete File\')" title="Delete" class="icon icon-delete"></a>';
    del = (user_type == 'student') ? '<span class = "icon" ></span>' : del;

    return download + del;
}

function lessonRules()
{
    $('#lesson_form').validate({
        debug: true,
        rules: {
           lesson_name:{
                required: true
            }
        },
        messages: {
            lesson_name:  $.Msgs('required')
        },
        onkeyup: false,
        focusInvalid: true,
        errorElement: 'span',
        errorPlacement: function(error, element) {
            $(element).next('.icon-error').remove();
            $(element).parent().append('<span class="icon icon-error"></span>');
            var icon = $(element).next('span');

            icon.qtip({
                content: $(error).text(),
                position: {
                    corner: {
                        target: 'rightTop',
                        tooltip: 'leftBottom'
                    }
                },
                hide: {
                    when: 'inactive',
                    delay: 1500
                },
                style: 'fielderror'
            });
        },
        highlight: function(element, errorClass) {
            $(element).parent().addClass('ui-state-error ui-corner-all');
        },
        unhighlight: function(element, errorClass) {
            $(element).next('.icon-error').remove();
            $(element).parent().removeClass('ui-state-error ui-corner-all');
        },
        submitHandler: function(form) {
            saveLesson();
        }
    });

}

function lessonFilesRules()
{
    $('#lessonfiles_form').validate({
        debug: true,
        rules: {
           file_name:{
                required: true
            }
        },
        messages: {
            file_name:  $.Msgs('required')
        },
        onkeyup: false,
        focusInvalid: true,
        errorElement: 'span',
        errorPlacement: function(error, element) {
            $(element).next('.icon-error').remove();
            $(element).parent().append('<span class="icon icon-error"></span>');
            var icon = $(element).next('span');

            icon.qtip({
                content: $(error).text(),
                position: {
                    corner: {
                        target: 'rightTop',
                        tooltip: 'leftBottom'
                    }
                },
                hide: {
                    when: 'inactive',
                    delay: 1500
                },
                style: 'fielderror'
            });
        },
        highlight: function(element, errorClass) {
            $(element).parent().addClass('ui-state-error ui-corner-all');
        },
        unhighlight: function(element, errorClass) {
            $(element).next('.icon-error').remove();
            $(element).parent().removeClass('ui-state-error ui-corner-all');
        },
        submitHandler: function(form) {
            form.submit();
        }
    });

}

function saveLesson(){
    $.loading('Lesson - Adding Lesson');
    
    $.ajax({
        url: siteUrl + 'lesson/submit',
        type: 'post',
        data: $('#lesson_form').serialize(),
        success: function(response) {
            
            if (response)
            {
                window.location = siteUrl + 'lesson/list_records/' + $('#group_id').val();
            }
        }
    });
}




function deleteLesson(id)
{
    $.loading('Lesson - Deleting Record');

    $.ajax({
        url: siteUrl + 'lesson/delete/' + id,
        type: 'post',
        dataType: 'json',
        success: function(response) {
            if (response == true)
            {
                window.location = siteUrl + 'lesson/list_records/' + $('#group_id').val();
            }

        }
    });
}

function deleteLessonFile(id)
{
    $.loading('Lesson - Deleting Record');

    $.ajax({
        url: siteUrl + 'lesson/delete_file/' + id,
        type: 'post',
        dataType: 'json',
        success: function(response) {
            if (response == true)
            {
                window.location = siteUrl + 'lesson/lesson_files/' + $('#group_id').val() + '/' +$('#lesson_id').val();
            }

        }
    });
}


function filterLessonGroup(){

    $('#lessonGrid').setGridParam({
    postData:
        {
            search: $('#search').val()
        }
    }).trigger('reloadGrid');
}